//
//  UIApplication+ZXCategory.m
//  MobileCaiLocal
//
//  Created by simon on 2019/11/28.
//  Copyright © 2019 com.Chs. All rights reserved.
//

#import "UIApplication+ZXCategory.h"


@implementation UIApplication (ZXCategory)

//定义状态栏区域的框架矩形。如果状态栏隐藏，则statusBarFrame属性的值为CGRectZero。
+ (CGFloat)zx_safeAreaStatusBarHeight
{
    CGFloat statusBarHeight = 0;
    if (@available(iOS 13.0, *)) {
        NSSet <UIScene *>*connectedScenes = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *scene = nil;
        if (![UIApplication sharedApplication].supportsMultipleScenes) {
            scene = (UIWindowScene *)[[connectedScenes allObjects] firstObject];
        }else{
            for (UIWindowScene * windowScene in connectedScenes) {
                if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                    scene = windowScene;
                }
            }
        }
        statusBarHeight = scene.statusBarManager.statusBarFrame.size.height;
    }else
    {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
#pragma clang diagnostic pop

    }
    return statusBarHeight;
}

+ (CGFloat)zx_safeAreaLayoutNormalBottom
{
    CGFloat safeAreaBottomHeight = 0;
    if (@available(iOS 13.0, *)) {
        
        NSSet <UIScene *>*connectedScenes = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *scene = nil;
        if (![UIApplication sharedApplication].supportsMultipleScenes) {
            scene = (UIWindowScene *)[[connectedScenes allObjects] firstObject];
        }else{
            for (UIWindowScene * windowScene in connectedScenes) {
                if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                    scene = windowScene;
                }
            }
        }
        id sceneDelegate = scene.delegate;
        if (!sceneDelegate){
            UIEdgeInsets areaInset = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
            safeAreaBottomHeight = areaInset.bottom;
        }
        else
        {
            if ([sceneDelegate respondsToSelector:@selector(setWindow:)])
            {
                safeAreaBottomHeight = [sceneDelegate window].safeAreaInsets.bottom;
            }
        }
    }
    else
    {
        if (@available(iOS 11.0, *)) {
            UIEdgeInsets areaInset = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
            if (areaInset.bottom >0) {
                safeAreaBottomHeight = areaInset.bottom;
            }
        }
    }
    return safeAreaBottomHeight;
}


+ (CGFloat)zx_safeAreaNavBarHeight{
    CGFloat height = UIApplication.zx_safeAreaStatusBarHeight + 44;
    return height;
}

+ (CGFloat)zx_safeAreaTabBarHeight{
    CGFloat height = UIApplication.zx_safeAreaLayoutNormalBottom + 49;
    return height;
}

@end
